//
//  QOrderRecordVC.m
//  QoilPublic
//
//  Created by 杨凯 on 2017/2/23.
//  Copyright © 2017年 杨凯. All rights reserved.
//

#import "QOrderRecordVC.h"
#import "QThreeOrderCell.h"
#import "QOrderAndDateCell.h"
#import "QCuOrderModel.h"
#import "QOrderListModel.h"
@interface QOrderRecordVC ()<UITableViewDataSource,UITableViewDelegate>
{
    NSInteger _index;
    NSMutableArray *_dataArr;
    BOOL _today;
    UILabel *_titleLab;
}
@property (nonatomic,strong)UITableView *tbView;
@property (nonatomic,strong)QOrderListModel *orderListModel;
@end

@implementation QOrderRecordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"订单明细";
    _dataArr = [NSMutableArray array];
    _today = NO;
    [self createUI];
    [self requestData];
    WS(wSelf);
    _tbView.mj_header = [QViewUtility showHeadRefresh:^{
        [_tbView.mj_footer resetNoMoreData];
        [wSelf requestData];
    }];
    
    _tbView.mj_footer = [QViewUtility showFootRefresh:^{
        [wSelf requestMoreData];
    }];
}
- (void)requestData{
    _index = 1;
    NSString *start = [NSString stringWithFormat:@"%ld",(long)_index];
    NSString *url = [QParamService urlWithAction:@"getMyCommissionOrders"];
    NSDictionary *para =[QParamService getMyCommissionOrdersWithCommissionId:@"" Start:start PageSize:KpageSize];
    [QViewUtility showMBHudToView:self.view hideBg:YES];
    [QViewUtility dismissNoDataViewInVC:self];
    [QViewUtility disMissNoNetWorkInVC:self];
    [[QHttpManager sharedManager] POST:url parameterMD5:para completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [_tbView.mj_header endRefreshing];
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            if([QUserInfo sharedInfo].isHaveNetWork == NO){
                [QViewUtility showNoNetWorkInView:_tbView];
            }
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            [_dataArr removeAllObjects];
        
            _orderListModel =[QOrderListModel objectFromDict:basModel.items];
            [_dataArr addObjectsFromArray:_orderListModel.orders];
            
            if(_dataArr.count == 0){
                [QViewUtility showNoDataViewInView:_tbView string:@""];
            }else{
                if(_orderListModel.orders.count < 10){
                    [_tbView.footer noticeNoMoreData];
                }
            }
            [_tbView reloadData];
            
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
}

- (void)requestMoreData{
    _index++;
    NSString *start = [NSString stringWithFormat:@"%ld",(long)_index];
    NSString *url = [QParamService urlWithAction:@"getMyCommissionOrders"];
    NSDictionary *para =[QParamService getMyCommissionOrdersWithCommissionId:@"" Start:start PageSize:KpageSize];
    [[QHttpManager sharedManager] POST:url parameterMD5:para completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [_tbView.footer endRefreshing];
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        
        if([basModel.result isEqualToString:@"success"]){
            _orderListModel = [QOrderListModel objectFromDict:basModel.items];
            [_dataArr addObjectsFromArray:_orderListModel.orders];
            if(_orderListModel.orders.count < 10){
                [_tbView.mj_footer noticeNoMoreData];
                //[QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"没有更多了" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            }
            [_tbView reloadData];
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
    
}


- (void)createUI{
    [self.view addSubview:self.tbView];
    [self.tbView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0);
    }];
    
    UIView *nav = [[UIView alloc]initWithFrame:CGRectMake(0, -44, kScreenWidth, 44)];
    nav.backgroundColor = [QColorUtility defaultBlueColor];
    [self.view addSubview:nav];
    
    [self.tbView registerNib:[UINib nibWithNibName:@"QThreeOrderCell" bundle:nil] forCellReuseIdentifier:@"QThreeOrderCell"];
    
    [self.tbView registerNib:[UINib nibWithNibName:@"QOrderAndDateCell" bundle:nil] forCellReuseIdentifier:@"QOrderAndDateCell"];
    
    UIView *header =[[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
    _titleLab =[QViewUtility createLabelWithFrame:CGRectMake(20, 0, kScreenWidth-20, 50) font:Ksize12 textAlignment:0 textColor:[QColorUtility defaultDeepGrayColor] text:@""];
    _titleLab.numberOfLines =0;
    [header addSubview:_titleLab];
    
    self.tbView.tableHeaderView =header;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 94;
    }
    QCuOrderModel * model = _dataArr[indexPath.row];
    QCuOrderModel * model1 =_dataArr[indexPath.row -1];
    if (![model.addTime isEqualToString:model1.addTime]) {
        return 94;
    }
    return 56;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSecton:(NSInteger)section
{
    //return 44;
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 6;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (_dataArr.count) {
        _titleLab.text = @"APP端仅显示本月客户下单明细，查看更多订单请联系客服!";
    }

    return _dataArr.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QCuOrderModel * model22 = _dataArr[indexPath.row];
    if ([_orderListModel.now isEqualToString:model22.addTime]) {
        model22.addTime = @"今天";
        _today = YES;
    }

    if (indexPath.row == 0) {
        QOrderAndDateCell * cell =[tableView dequeueReusableCellWithIdentifier:@"QOrderAndDateCell"];
        cell.model = model22;
        cell.lineView.hidden = YES;
        return cell;
    }
    
    QCuOrderModel * model = _dataArr[indexPath.row];
    QCuOrderModel * model1 =_dataArr[indexPath.row -1];
    if (![model.addTime isEqualToString:model1.addTime]) {
        QOrderAndDateCell * cell =[tableView dequeueReusableCellWithIdentifier:@"QOrderAndDateCell"];

        cell.model = model22;
        cell.lineView.hidden = NO;


        return cell;
    }else{
        QThreeOrderCell * cell= [tableView dequeueReusableCellWithIdentifier:@"QThreeOrderCell" forIndexPath:indexPath];
        cell.model = model22;


        return cell;
    }

    
}



#pragma mark - Getter/Setter
- (UITableView*)tbView {
    if (!_tbView) {
        _tbView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tbView.dataSource = self;
        _tbView.delegate = self;
        _tbView.backgroundColor = [UIColor clearColor];
        _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tbView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
